
import Vue from 'vue'
import VueRouter from 'vue-router'
import localStorageService from '../ddd/localStorage.service'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

import defaultRouter from './default.router.js'
import systemRouter from './system.router.js'
import webRouter from './web.router.js'

Vue.use(VueRouter)

const routerList = [
    ...defaultRouter,
    ...systemRouter,
    ...webRouter
]

const routes = [...routerList]

const router = new VueRouter({
    mode: 'history',
    scrollBehavior: () => ({ y: 0 }),
    routes: routes
})

// 防止重复点击路由
const VueRouterPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (to) {
    return VueRouterPush.call(this, to).catch(err => err)
}

router.beforeEach((to, from, next) => {
    document.title = 'Md通用管理系统'

    NProgress.start();

    // const leftMenus = []
    // const routers = [...systemRouter, ...panoramaRouter, ...orderRouter]

    // routers.filter(e => e.visible).forEach((item, index) => {
    //     const params = {
    //         value: index + 1,
    //         ...item
    //     }

    //     params.children = []

    //     if (item.children && item.children.length > 0) {
    //         item.children.filter(e => e.meta.visible).forEach((sub, ind) => {
    //             const subParams = {
    //                 value: ind + 1,
    //                 ...sub
    //             }
    //             params.children.push(subParams)
    //         })
    //     }
    //     leftMenus.push(params)
    // })

    // store.state.mdShared.leftMenus = leftMenus

    if (to.path == '/login') {
        next()
    } else {
        if (localStorageService.getItem('Authorization')) {
            next()
        } else {
            window.location.href = '/login'
            next()
        }
    }
})

router.afterEach(() => {
    NProgress.done();
})

export default router